package cn.tedu.egnotice1.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeListQuery;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String AddNotice(NoticeAddParam noticeAddParam){
        System.out.println("notictAddParam:"+noticeAddParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setIs_delete(0);
        noticeMapper.insertNotice(notice);
        return "添加成功";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> NoticeListVOS(NoticeListQuery noticeListQuery){
        System.out.println("noticeListQuery:"+noticeListQuery);
        List<NoticeListVO> noticeListVOS=noticeMapper.noticeListvo1(noticeListQuery);
        return noticeListVOS;
    }
    @GetMapping("/v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO detailInfo (Long id){
        System.out.println("id:"+id);
        return noticeMapper.noticeDetailInfoVO(id);
    }


    @DeleteMapping("/v1/notice/del")
    @ResponseBody
    public String delNotice(Long id){
        System.out.println("id:"+id);
        int rows=noticeMapper.deleteNoticeById(id);
        if (rows>0){
            return "删除成功";
        }else {
            return "咨询不存在，请重新输入";
        }
    }

    @PutMapping("/v1/notice/upd")
    @ResponseBody
    public String update(NoticeUpdateParam param){
        int rows=noticeMapper.updateNoticeById(param);
        if (rows>0){
        return "更新完成";
        } else {
            return "咨询不存在，请重新输入";
        }


    }




}
